package com.lucher.tangstore.controller;

import com.lucher.tangstore.dao.*;
import com.lucher.tangstore.po.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/product")  
public class ProductController extends BaseController{
	@Autowired
	private ProductMapper productDao;
	
	/**
	 * 查询信息
	 */
	@RequestMapping(value = "/findByList")
	public String findByList(Product form) {
		return "product/findProductList";
	}
	
	/**
	 * 查询信息Json
	 */
	@ResponseBody
	@RequestMapping(value = "/findListJson")
	public Map<String, Object> findListJson(Product form) {
		//封装JSon的Map
		Map<String, Object> result = new HashMap<String, Object>(); 
		result.put("code", "0");
		List<HashMap<String, Object>> objectlist = new ArrayList<HashMap<String, Object>>();
		
		Integer total = productDao.findByCount(form);
		List<Product> list = productDao.findByListForPage(form);
		
		for (Product product : list) {
			HashMap<String, Object> objectMap = new HashMap<String, Object>();
			
			objectMap.put("id", product.getId());
			objectMap.put("productcode", product.getProductcode());
			objectMap.put("productname", product.getProductname());
			objectMap.put("productunit", product.getProductunit());
			objectMap.put("depotamount", product.getDepotamount());
			objectMap.put("remark", product.getRemark());
			objectlist.add(objectMap);
		}
		
		result.put("count", total);
		result.put("data", objectlist);
		return result;
	}

	/**
	 * 添加信息初始化
	 * 
	 * @return
	 */
	@RequestMapping(value = "/addInit")
	public String addInit(Product form,Model model) {
		//查询所有的工序信息
		//List<Workorder> workorderlist = workorderDao.findByList(new Workorder());
		//model.addAttribute("workorderlist", workorderlist);
		return "product/addProduct";
	}

	/**
	 * 新增
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value = "/save")
	public Map<String,Object>  save(Product form) {
		
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		
		if ("".equals(form.getProductcode())) {
			responseJson.put("flag", "0"); //操作失败
			responseJson.put("msg", "操作失败，请输入产品编码");
			return responseJson;
		} else {
			Product oldProduct = productDao.findByProductcode(form.getProductcode());
			if (oldProduct != null) {
				responseJson.put("flag", "0");//操作失败
				responseJson.put("msg", "操作失败，此产品编码已经存在");
				return responseJson;
			}
		}
		
		if ("".equals(form.getProductname())) {
			responseJson.put("flag", "0"); //操作失败
			responseJson.put("msg", "操作失败，请输入产品名称");
			return responseJson;
		} else {
			Product product = productDao.findByProductname(form.getProductname());
			if (product != null) {
				responseJson.put("flag", "0");//操作失败
				responseJson.put("msg", "操作失败，此产品名称已经存在");
				return responseJson;
			}
		}
		
//		String ordercodes = form.getOrdercodes();
//		if(StringUtils.isBlank(ordercodes)){
//			responseJson.put("flag", "0");//操作失败
//			responseJson.put("msg", "操作失败，请先选择工序信息");
//			return responseJson;
//		}


		//保存产品信息
		Integer productId = productDao.insertSelective(form);

		responseJson.put("flag", "1");//操作成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}

	/**
	 * 更新初始化
	 */
	@RequestMapping(value = "/updateInit")
	public String updateInit(Product form,Model model) {
		Product product = productDao.selectByPrimaryKey(form.getId());
		form.setProduct(product);
		
		return "product/updateProduct";
	}

	/**
	 * 更新
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value = "/update")
	@Transactional
	public Map<String,Object> update(Product form) {
		
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		
		Product product_old = productDao.selectByPrimaryKey(form.getId());
		
		if ("".equals(form.getProductcode())) {
			responseJson.put("flag", "0"); //添加失败
			responseJson.put("msg", "操作失败，产品编码不能为空");
			return responseJson;
		} else {
			Product oldProduct = productDao.findByProductcode(form.getProductcode());
			if (oldProduct != null && !oldProduct.getId().equals(form.getId())) {
				responseJson.put("flag", "0"); //添加失败
				responseJson.put("msg", "操作失败，产品编码已经存在");
				return responseJson;
			}
		}
		
		if ("".equals(form.getProductname())) {
			responseJson.put("flag", "0"); //添加失败
			responseJson.put("msg", "操作失败，产品名称不能为空");
			return responseJson;
		} else {
			Product oldProduct = productDao.findByProductname(form.getProductname());
			if (oldProduct != null && !oldProduct.getId().equals(form.getId())) {
				responseJson.put("flag", "0"); //添加失败
				responseJson.put("msg", "操作失败，产品名称已经存在");
				return responseJson;
			}
		}
		
		// 修改信息
		int flag = productDao.updateByPrimaryKeySelective(form);

		
		responseJson.put("flag", "1");//
		responseJson.put("msg", "操作成功");
		return responseJson;
	}
    
	/**
	 * 删除
	 * @return
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value="/delete")
	public Map<String,Object> delete(Product form) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		//删除产品
		productDao.deleteByPrimaryKey(form.getId());

		responseJson.put("flag", "1");//删除成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}
	
	/**
	 * 批量删除
	 * @return
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value="/deleteBatch")
	public Map<String,Object> deleteBatch(@RequestBody List<Product> formlist) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		for (Product product : formlist) {
			//删除材料
			productDao.deleteByPrimaryKey(product.getId());
		}

		responseJson.put("flag", "1");//删除成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}
	
	/**
     * 获取下拉列表框Json值
     */
	@ResponseBody //此标签表示返回的数据不是html标签的页面，而是其他某种格式的数据时（如json、xml等）使用；
	@RequestMapping(value="/getProductCombogridJson")
	public List<Map<String,Object>> getProductCombogridJson(Product form) {  
	    List<Product> productList = productDao.findByList(form);
	    List<Map<String, Object>> productCombogridJson = new ArrayList<Map<String, Object>>();
	    //封装属性结构信息
	    for(Product product:productList){  
	    	    HashMap<String,Object> productMap = new HashMap<String, Object>();
	    	    productMap.put("id", product.getId());
	    	    productMap.put("productcode", product.getProductcode());
	    	    productMap.put("productname", product.getProductname());
	    	    productMap.put("productsource", product.getProductsource());
	    	    productMap.put("depotamount", product.getDepotamount());
	    	    productMap.put("productunit", product.getProductunit());
	    	    productCombogridJson.add(productMap);  
	    }  
	   return productCombogridJson;
	}  
	
}
